#  coding=utf-8
from .. import db

from .base import BaseModel


class ClubPassword(BaseModel):
    __tablename__ = 'club_password'
    id = db.Column(db.BIGINT, primary_key=True)
    club_id = db.Column(db.BIGINT, db.ForeignKey('club.id'), doc='俱乐部')
    access_club = db.relationship('Club', foreign_keys=club_id,
                                  backref=db.backref('club_password'))
    access_password = db.Column(db.String(100), doc='访问密码')
    owner_id = db.Column(db.Integer, db.ForeignKey('user.id'), doc='访问者id')
    owner = db.relationship('User', foreign_keys=owner_id,
                            backref=db.backref('club_password'))

    def __init__(self, **kwargs):
        super(ClubPassword, self).__init__(**kwargs)

    def __repr__(self):
        return '<clubPassword %r>' % self.id

    @staticmethod
    def insert_club_password(club, accessor, password):
        try:
            object_ins = ClubPassword(
                club_id=club.id,
                club_author_id=club.author.id,
                access_password=password,
                owner_id=accessor.id
            )
            db.session.add(object_ins)
            db.session.commit()
            return True, object_ins
        except:
            return False, None
